<template>
  <div id="root">
    <div class="todo-container">
      <div class="todo-wrap">
        <Hearder :updataAdd="updataAdd" />
        <List :arrlist="arrlist" :updataDelete="updataDelete" :updataChecked="updataChecked" />
        <Footer :updataFinish="updataFinish" :arrlist="arrlist" :updatatodocheckedAll="updatatodocheckedAll" />
      </div>
    </div>
  </div>
</template>

<script>
import Hearder from './components/Hearder.vue'
import List from './components/List.vue'
import Footer from './components/Footer.vue'

export default {
  name: 'App',
  components: { Hearder, List, Footer },
  data() {
    return {
      // arrlist: [
      //   { id: "safasfas01", title: '吃', done: false },
      //   { id: "safasfas02", title: '喝', done: false },
      //   { id: "safasfas03", title: '嬲', done: true },
      // ]
      arrlist: JSON.parse(localStorage.getItem('arrlist')) || [],
    }
  },
  watch: {
    arrlist: {
      deep: true,
      handler(val) {
        // 将本地的数据存储在localStorage中
        localStorage.setItem('arrlist', JSON.stringify(val))
      }
    }
  },
  methods: {
    //增加功能的函数回调
    updataAdd(val) {
      // 往数组中添加数据
      this.arrlist.push(val)
    },
    // 删除功能的函数回调
    updataDelete(id) {
      // console.log('@sddd' + id);
      this.arrlist = this.arrlist.filter(item => item.id !== id)
    },
    // 勾选功能的函数回调
    updataChecked(id) {
      // console.log('@xxx');
      // 拿旧的数据去代替新生成的数据
      this.arrlist.forEach((item) => {
        // 判断如果数据中的id与传过来的id相等,那么就将他取反(勾选上)
        if (item.id === id) { item.done = !item.done }
      })
    },
    // 清空已完成任务函数的回调
    updataFinish() {
      // 遍历数据,留下done为false的数据,把是true的数据删除掉
      this.arrlist = this.arrlist.filter((item) => {
        return !item.done
      })
    },
    // 完成全部 / 取消全部函数的回调
    updatatodocheckedAll(val) {
      // console.log('@', 111);
      // 遍历数据,将数组中的每一项的done赋值为val
      this.arrlist.forEach((item) => { item.done = val })
    }
  },
}
</script>

<style>
/*base*/
body {
  background: #fff;
}

.btn {
  display: inline-block;
  padding: 4px 12px;
  margin-bottom: 0;
  font-size: 14px;
  line-height: 20px;
  text-align: center;
  vertical-align: middle;
  cursor: pointer;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2), 0 1px 2px rgba(0, 0, 0, 0.05);
  border-radius: 4px;
}

.btn-danger {
  color: #fff;
  background-color: #da4f49;
  border: 1px solid #bd362f;
}

.btn-danger:hover {
  color: #fff;
  background-color: #bd362f;
}

.btn:focus {
  outline: none;
}

.todo-container {
  width: 600px;
  margin: 0 auto;
}

.todo-container .todo-wrap {
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 5px;
}
</style>
